<?php

/**
 * This is the model class for table "{{banner_master}}".
 *
 * The followings are the available columns in table '{{banner_master}}':
 * @property integer $banner_id
 * @property string $page_url
 * @property integer $banner_position
 * @property string $start_date
 * @property string $end_date
 * @property integer $banner_code
 * @property integer $active
 * @property integer $created_by
 * @property string $created_date
 * @property integer $edited_by
 * @property string $edited_date
 * @property integer $page_id
 */
class BannerMaster extends CActiveRecord
{
	public $banner_type;
	public $banner_widget;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return BannerMaster the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{banner_master}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('page_url, banner_position, active, page_id,type,ip_location', 'required'),
			array('banner_position, active, created_by, edited_by, page_id', 'numerical', 'integerOnly'=>true),
			array('page_url', 'length', 'max'=>200),
			array('image,widget_name', 'length', 'max'=>500),
			array('end_date', 'compare', 'compareAttribute'=>'start_date', 'operator'=>'>','message'=>'End date must be greater than Start date'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('banner_id, page_url, banner_position, image, start_date, end_date, banner_code, active, created_by, created_date, edited_by, edited_date, page_id', 'safe', 'on'=>'search'),
			array('edited_by,created_by', 'default', 'value' => ApplicationSessions::run()->read('id'), 'setOnEmpty' => false, 'on' => 'insert'),
			array('edited_by', 'default', 'value' => ApplicationSessions::run()->read('id'), 'setOnEmpty' => false, 'on' => 'update'), 
			array('edited_date,created_date', 'default', 'value' => new CDbExpression('NOW()'), 'setOnEmpty' => false, 'on' => 'insert'), 
			array('edited_date', 'default', 'value' => new CDbExpression('NOW()'), 'setOnEmpty' => false, 'on' => 'update'), 
			array('image', 'file', 'types' => 'jpeg,jpg,gif,png', 'allowEmpty' => true,),
			array('page_url','filter','filter' => 'strip_tags'),
			
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'banner_id' => 'Banner',
			'page_url' => 'Page Url',
			'image'		=>	'Image',
			'banner_position' => 'Banner Position',
			'start_date' => 'Start Date',
			'end_date' => 'End Date',
			'banner_code' => 'Banner Code',
			'active' => 'Active',
			'created_by' => 'Created By',
			'created_date' => 'Created Date',
			'edited_by' => 'Edited By',
			'edited_date' => 'Edited Date',
			'page_id' => 'Page',
			'type' => 'Banner Type',
			'ip_location'=>'Location'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($rhs = null)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('banner_id',$this->banner_id);
		$criteria->compare('page_url',$this->page_url,true);
		if (!empty($rhs))
		{
			$criteria->compare('banner_position','10',true,'OR');
		}
		else
		$criteria->compare('banner_position',$this->banner_position);
		
		$criteria->compare('start_date',$this->start_date,true);
		$criteria->compare('end_date',$this->end_date,true);
		$criteria->compare('banner_code',$this->banner_code);
		$criteria->compare('active',$this->active);
		$criteria->compare('created_by',$this->created_by);
		$criteria->compare('created_date',$this->created_date,true);
		$criteria->compare('edited_by',$this->edited_by);
		$criteria->compare('edited_date',$this->edited_date,true);
		$criteria->compare('page_id',$this->page_id);
		$criteria->compare('type',$this->type);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public static function getBanner($type, $arrParams = array())
	{
	
        switch ($type)
        {
            case 'BANNER':
				
        		$locationip = ApplicationConfig::getUserLocationByIp();					
                return BannerMaster::model()->find(array('condition'=>'active= 1 AND banner_position='.$arrParams['pos'].' AND ip_location = "'.$locationip.'"'));
                break;
        }
	}
	public static function getList($type, $arrParams = array())
	{
	
        switch ($type)
        {
            case 'RHS':
				$locationip = ApplicationConfig::getUserLocationByIp();	
                return BannerMaster::model()->findAll(array('condition'=>'active= 1 AND banner_position='.$arrParams['pos'].' AND ip_location = "'.$locationip.'"','order'=>'sort_order'));
                break;
                
            case 'ALL-BANNER-PAGES':
                return BannerMaster::model()->findAll(array("condition"=>"banner_position='".$arrParams['id']."'"));
                break;   
        }
	}
	public function getContent($arrParams = array())
	{
		if($arrParams['type'] == "image")
		{
		    $return = CHtml::image(ApplicationConfig::app()->params["url_path"]["bannersSmall"].$arrParams['image'],"",array("style" => "width: 50px;height:40px;","align"=>"center"));
		}
		else if($arrParams['type'] == "widget")
		{
			$return = $arrParams['widget'];
		}
		else if($arrParams['type'] == "code")
		{
			$return = $arrParams['code'];
		}
		
		return $return;
	}
	
	
}